package com.wp.redisSpringBoot01;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
class RedisSpringBoot01ApplicationTests02 {

    //自动注入StringRedisTemplate对象，该类中封装了很多api方法--redis服务
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 默认RedisTemplate它的key和value的序列化都是使用的JdkSerializationRedisSerializer方式，该序列化要求类必须实现Serializable接口。
     */
    @Test
    public void test1() {
        //key都是String类型，指定String序列化方式
        //redisTemplate.setKeySerializer(new StringRedisSerializer());
        //value是对象类型，指定Json序列化方式
        //redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        ValueOperations forValue = redisTemplate.opsForValue();
        forValue.set("k1",new Student(1,"张三","男"));
        Object k1 = forValue.get("k1");
        System.out.println("k1 = " + k1);
    }

    @Test
    public void test2() {
        HashOperations forHash = redisTemplate.opsForHash();
        Map<String , Student> map = new HashMap<>();
        map.put("S1",new Student(2,"李四","男"));
        forHash.putAll("k2",map);
    }

}
